package com.itfreer.ui.base.domain;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itfreer.ui.R;
import com.itfreer.ui.base.DialogCloseListener;
import com.itfreer.ui.base.DialogResult;
import com.itfreer.ui.base.api.IDataView;
import com.itfreer.ui.base.api.IEditTextValidator;
import com.itfreer.ui.base.api.IPower;
import com.itfreer.ui.base.api.OnValueChange;
import com.itfreer.utils.PixelUtils;

import java.util.Map;

/**
 * 定义字典单选择控件
 */
public class DomainRadioView extends LinearLayout
        implements View.OnClickListener, IDataView, IPower, IEditTextValidator {

    private String pid;
    private Integer loadlevel = 0;
    private String nextUI;
    private String fieldName;
    private Object bindData;
    private DomainRadioDialog radioDialog;
    private String dialogTitle = "单选项";
    private TextView textView;
    private String securityID;
    private boolean securityVisible=true;
    private OnValueChange onValueChange;
    private Object bindValue;
    private String dicName = null;
    private boolean readonly = false;
    private String textGravity = "";
    private String validatorName;
    private String validateTitle;
    private IDataView nextUIEx;

    public void setOnValueChange(OnValueChange onValueChange) {
        this.onValueChange = onValueChange;
    }

    @Override
    public IDataView getNextUIEx() {
        return nextUIEx;
    }

    @Override
    public void setNextUIEx(IDataView pDataView) {
        nextUIEx = pDataView;
    }

    public DomainRadioView(Context context) {
        super(context);
        setView(context);
    }

    public DomainRadioView(Context context, AttributeSet attrs){
        super(context, attrs);
        getAttrs(context, attrs);
        setView(context);
    }

    public DomainRadioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        setView(context);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        this.fieldName = a.getString(R.styleable.SimpleView_fieldName);
        this.pid = a.getString(R.styleable.SimpleView_pid);
        this.securityID = a.getString(R.styleable.SimpleView_securityID);
        this.loadlevel = a.getInteger(R.styleable.SimpleView_loadLevel, 0);
        this.nextUI = a.getString(R.styleable.SimpleView_nextUI);
        this.dicName = a.getString(R.styleable.SimpleView_dicName);
        this.readonly = a.getBoolean(R.styleable.SimpleView_readonly, false);
        this.textGravity = a.getString(R.styleable.SimpleView_textGravity);
        validatorName = a.getString(R.styleable.SimpleView_validatorName);
        validateTitle = a.getString(R.styleable.SimpleView_validateTitle);
    }

    /**
     * 设置绑定的依据对象
     * @param name
     */
    @Override
    public void setFieldName(String name){
        this.fieldName = name;
    }

    /**
     * 获取绑定的依据对象
     * @return
     */
    @Override
    public String getFieldName(){
        return fieldName;
    }

    /**
     * 设置绑定的值
     * @param value
     */
    @Override
    public void setFieldValue(Object value){
        this.bindValue = value;
        bindFieldValue();

        if(onValueChange!=null){
            if(value==null){
                onValueChange.onChange("");
            }else{
                onValueChange.onChange(value.toString());
            }
        }
    }

    private void bindFieldValue(){
        if(bindValue!=null){
            radioDialog.setCheckedValue(bindValue);
            setValueText();
        } else {
            textView.setText("");
        }
    }

    /**
     * 获取当前的值
     * @return
     */
    @Override
    public Object getFieldValue(){
        return radioDialog.getCheckedValue();
    }

    /**
     * 设置绑定的数据
     * @param data
     */
    @Override
    public void setBindData(Object data){
        bindData = data;
        radioDialog.setBindData(bindData);

        // 重新绑定值
        bindFieldValue();
    }

    /**
     * 获取绑定的数据
     * @return
     */
    @Override
    public Object getBindData(){
        return bindData;
    }

    /**
     * 设置标题
     * @param dialogTitle
     */
    public void setDialogTitle(String dialogTitle){
        this.dialogTitle = dialogTitle;
        radioDialog.setDialogTitle(dialogTitle);
    }

    public String getDialogTitle(){
        return dialogTitle;
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){
        View view = View.inflate(context, R.layout.com_itfreer_core_ui_base_inputitem, null);

        textView = (TextView)view.findViewById(R.id.com_itfreer_core_ui_base_datepickerview_textview);
        textView.setTextSize(PixelUtils.dpToPx(getContext(), 6));
        if(textGravity!=null && this.textGravity.equals("right")) {
            textView.setGravity(Gravity.RIGHT);
        }
        setReadonly(readonly);

        LayoutParams param =
                new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(view, param);

        radioDialog = new DomainRadioDialog(this.getContext());
        radioDialog.setDialogTitle(dialogTitle);
        radioDialog.setDialogCloseListener(new DialogCloseListener() {
            @Override
            public void OnClosed(Object send, DialogResult result) {
                if (result == DialogResult.Ok) {
                    setValueText();
                    bindValue = radioDialog.getCheckedValue();

                    if(onValueChange!=null){
                        onValueChange.onChange(radioDialog.getCheckedValue().toString());
                    }
                }
            }
        });
    }

    protected void setValueText(){
        if(bindData instanceof Map && radioDialog.getCheckedValue()!=null){
            Map data = (Map)bindData;
            if(data.containsKey(radioDialog.getCheckedValue())){
                String text = data.get(radioDialog.getCheckedValue()).toString();
                textView.setText(text.trim());
            }
        }
    }

    @Override
    public void onClick(View v) {
        radioDialog.show();
    }

    /**
     * 获取权限ID
     * @return
     */
    public String getSecurityID(){
        return securityID;
    }

    /**
     * 没有权限时，设置是否可见
     * @param visible
     */
    public void setSecurityVisible(boolean visible){
        securityVisible = visible;
        if(visible){
            this.setVisibility(View.VISIBLE);
        }else{
            // 表示不可见并且不占用空间
            this.setVisibility(View.GONE);
        }
    }

    /**
     * 数据绑定时的PID值，默认为0
     * @return
     */
    public String getPid(){
        if(this.pid!=null){
            return this.pid;
        }else{
            return "0";
        }
    }

    /**
     * 设置pid值
     * @param pid
     */
    public void setPid(String pid){
        this.pid = pid;
    }

    /**
     * 获取子级加载级别
     * @return
     */
    public Integer getLoadLevel() {
        return this.loadlevel;
    }

    public void setLoadlevel(Integer loadlevel) {
        this.loadlevel = loadlevel;
    }

    /**
     * 多级联动配置，获取下一级UI控件
     * @return
     */
    public String getNextUI() {
        return this.nextUI;
    }

    /**
     * 设置字典名称
     * @param dicName
     */
    public void setDicName(String dicName) {
        this.dicName = dicName;
    }

    /**
     * 获取字典名称
     * @return
     */
    public String getDicName() {
        return this.dicName;
    }

    public void setNextUI(String nextUI) {
        this.nextUI = nextUI;
    }

    public void setTextGravity(String textGravity) {
        this.textGravity = textGravity;
        if(textGravity!=null) {
            if(this.textGravity.equals("right")) {
                textView.setGravity(Gravity.RIGHT);
            } else if(this.textGravity.equals("center")) {
                textView.setGravity(Gravity.CENTER);
            } else if(this.textGravity.equals("left")) {
                textView.setGravity(Gravity.LEFT);
            }
        }
    }

    public void setReadonly(boolean readonly) {
        this.readonly = readonly;
        if(readonly) {
            textView.setOnClickListener(null);
            textView.setTextColor(Color.parseColor("#c1c1c1"));
        } else {
            textView.setOnClickListener(this);
            textView.setTextColor(Color.parseColor("#000000"));
        }
    }

    @Override
    public String getValidatorName() {
        return this.validatorName;
    }

    @Override
    public void setValidatorName(String name) {
        this.validatorName = name;
    }

    @Override
    public String getValidateTitle() {
        return this.validateTitle;
    }

    @Override
    public void setValidateTitle(String validateTitle) {
        this.validateTitle = validateTitle;
    }
}